//
//  ViewController.m
//  MacTestApp
//
//  Created by LiuLichen on 2022/3/22.
//

#import "ViewController.h"

#import <AVFoundation/AVFoundation.h>
#import <WebKit/WebKit.h>

@interface ViewController () <WKUIDelegate,
                              WKNavigationDelegate,
                              WKScriptMessageHandler>

@property(weak) IBOutlet NSView* controllerView;
@property(weak) IBOutlet NSView* contentView;
@property(weak) IBOutlet NSButton* backButton;
@property(weak) IBOutlet NSButton* forwardButton;
@property(weak) IBOutlet NSButton* reloadButton;
@property(weak) IBOutlet NSButton* cancelButton;
@property(weak) IBOutlet NSButton* goButton;
@property(weak) IBOutlet NSTextField* addressTextField;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Do any additional setup after loading the view.
    WKWebView* webview = [[WKWebView alloc] initWithFrame: CGRectMake(0, 0, self.contentView.bounds.size.width, self.contentView.bounds.size.height)];
    webview.UIDelegate = self;
    webview.navigationDelegate = self;
    [self.contentView addSubview: webview];
//    [webview loadRequest: [NSURLRequest requestWithURL: [NSURL URLWithString: @"https://www.baidu.com"]]];

    [self.reloadButton setTarget:self];
    [self.reloadButton setAction:@selector(onReload:)];
}


- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

- (void)onReload:(id)sender {
    BOOL granted = [self isValidToVisitPermission:AVMediaTypeAudio];
    NSLog(@"[LLC] granted = %d", granted);
}

- (BOOL)isValidToVisitPermission:(AVMediaType)mediaType {
    if (@available(macOS 10.14, *)) {
      __block BOOL bCanRecord = YES;

      AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:mediaType];
      NSLog(@"[LLC] status: %ld", status);

      switch (status) {
        case AVAuthorizationStatusNotDetermined: {
          [AVCaptureDevice requestAccessForMediaType:mediaType completionHandler:^(BOOL granted) {
            if (granted) {
              NSLog(@"[LLC] granted!");
            } else {
              NSLog(@"[LLC] not granted!");
            }
          }];
          break;
        case AVAuthorizationStatusAuthorized: {
          NSLog(@"[LLC] authorized!");
          bCanRecord = YES;
          break;
        }
        case AVAuthorizationStatusDenied: {
          NSLog(@"[LLC] denied!");
          bCanRecord = NO;
          break;
        }
        case AVAuthorizationStatusRestricted: {
          NSLog(@"[LLC] restricted!");
          bCanRecord = NO;
          break;
        }
        default:
          break;
        }
      }

      return bCanRecord;
    }

    return YES;
}

#pragma mark -- WKUIDelegate
- (nullable WKWebView*) webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures {
    return nil;
}

#pragma mark -- WKNavigationDelegate
- (void) webView:(WKWebView*)webView didStartProvisionalNavigation:(null_unspecified WKNavigation *)navigation {
    NSLog(@"WKWebView didStartProvisionalNavigation");
}

- (void) webView:(WKWebView*)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    NSLog(@"WKWebView didFinishNavigation");
}

#pragma mark -- WKScriptMessageHandler
- (void) userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {

}

@end
